package com.ymttest.business.service.productdata.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.json.JSONException;
import org.json.JSONObject;

import com.ymatou.iapi.productquery.parameter.ProductDetail.ProductInDetailBean;
import com.ymatou.iapi.productquery.service.GetProductInfoByProductIdCall;
import com.ymatou.iapi.sellerproduct.service.AddNewSellerProductCall;
import com.ymttest.business.service.ProductQueryCallService;
import com.ymttest.utils.FileUtil;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.compare.AssertHelper;
import com.ymttest.utils.compare.LogPathConf;
import com.ymttest.utils.compare.LoggerHelper;
import com.ymttest.utils.compare.LoggerInterface;

public class StageProduct {
	public static void main(String args[]) {

		int perSellerProdCount = 10;

		createProduct(perSellerProdCount);

	}

	public static void createProduct(int perSellerProdCount) {
		LogPathConf logPathConf = new LogPathConf(false);
		LoggerInterface logger = new LoggerHelper(logPathConf);
		AssertHelper.setLoggerInterface(logger);

		String userIdAndName = System.getProperty("user.dir") + File.separator
				+ "resource" + File.separator + "CompareData" + File.separator
				+ "userId_userName2.txt";
		List<String> userIdAndNameList = FileUtil.readFileAsLine(userIdAndName);

		String jsonFile = "com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_ForStageTest.json";
		JSONObject addProductBean = JsonBeanHelper.getJsonBean(jsonFile);

		for (int k = 0; k < userIdAndNameList.size(); k++) {
			String[] userIdAndNameAry = userIdAndNameList.get(k).split(",");
			int sellerId = Integer.parseInt(userIdAndNameAry[0]);
			JsonBeanHelper.updateJsonBean(addProductBean, MapUtil.hashMap(
					"UserID", sellerId, "LoginID", userIdAndNameAry[1],
					"VipPrice", 10, "NewCustomerPrice", 10));

			List<String> products = new ArrayList<>();
			for (int j = 0; j < perSellerProdCount; j++) {
				try {
					products.add(new AddNewSellerProductCall()
							.callAndGetReturnDataAndMgoNoCheck(addProductBean)
							.getJSONObject("Data").getString("ProductID"));
				} catch (JSONException e) {
					logger.failure("", e);
				}
			}

			YMTDateUtil.waitTime(5);
			for (int j = 0; j < perSellerProdCount; j++) {
				try {
					String productId = products.get(j);
					JSONObject prodDetail = new GetProductInfoByProductIdCall()
							.callAndGetReturnData(productId);

					ProductInDetailBean prodBean = ProductQueryCallService
							.getBeanFromJson(prodDetail.getJSONObject("Data")
									.getJSONObject("Product"),
									ProductInDetailBean.class);
					List<String> catalogList = prodBean.getCatalogList()
							.stream().map(x -> x.getCatalogId())
							.collect(Collectors.toList());

					catalogList.forEach(ele -> {
						String filePath = logPathConf.getFolderPath()
								+ File.separator + "ret.txt";
						FileUtil.writeAsLine(filePath, sellerId + "," + ele);
					});

				} catch (Exception e) {
					logger.failure("", e);
				}
			}
		}
	}
}
